package com.xiaoq.sites.repository.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.sites.entity.SitesAnnouncementEntity;
import com.xiaoq.sites.repository.IAnnouncementRepository;
import com.xiaoq.vo.PagingVo;
@Repository
public class AnnouncementRepositoryImpl implements IAnnouncementRepository {
	
	@Autowired
	private Datastore datastore;
	
	@Autowired
	private IdGenerator  idGenerator;

	@Override
	public List<SitesAnnouncementEntity> findAnnBySites(PagingVo pvo, String sitesId) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Query<SitesAnnouncementEntity> sitesAnnQuery = datastore.find(SitesAnnouncementEntity.class);
		if (sitesId != null) {
			sitesAnnQuery.field("sitesId").equal(sitesId);
			
		}
		sitesAnnQuery.field("pubStatus").equal("1");
		Long total = 0L;
		total = sitesAnnQuery.count();
		sitesAnnQuery.order("-pubTime").offset(pn).limit(ps);
		pvo.setTc(total);
		List<SitesAnnouncementEntity> content = sitesAnnQuery.asList();
		return content;

	}

	@Override
	public boolean addSitesAnnouncement(SitesAnnouncementEntity sitesAnnouncementEntity) {
		if(sitesAnnouncementEntity!=null){
			sitesAnnouncementEntity.setId(idGenerator.nextId());
			sitesAnnouncementEntity.setPubTime(System.currentTimeMillis());
			datastore.save(sitesAnnouncementEntity);
			return true;
		}
		return false;
	}
	@Override
	public boolean delSitesAnnoment(String annId) {
		if(StringUtils.isNotEmpty(annId)){
			datastore.delete(datastore.get(SitesAnnouncementEntity.class,annId));
			return true;
			
		}
		
		return false;
	}

	@Override
	public boolean invalSitesAnnounment(String annId) {
		if(annId!=null){
			Query<SitesAnnouncementEntity> annQuery= datastore.createQuery(SitesAnnouncementEntity.class)
					.field("_id").equal(annId);
			UpdateOperations<SitesAnnouncementEntity> sitesAnnUpdate = datastore.createUpdateOperations(SitesAnnouncementEntity.class)
					.set("pubStatus", "0");
			datastore.update(annQuery, sitesAnnUpdate);
			return true;
					
		}
		return false;
	}

	@Override
	public boolean editeSitesAnnounment(SitesAnnouncementEntity sitesAnnouncementEntity) {
		if (sitesAnnouncementEntity != null) {
			String annId = sitesAnnouncementEntity.getId();
			Query<SitesAnnouncementEntity> annQuery = datastore.createQuery(SitesAnnouncementEntity.class).field("_id")
					.equal(annId);
			UpdateOperations<SitesAnnouncementEntity> sitesAnnounementUpdate = datastore
					.createUpdateOperations(SitesAnnouncementEntity.class);
			if (sitesAnnouncementEntity.getSitesId() != null) {
				sitesAnnounementUpdate.set("sitesId", sitesAnnouncementEntity.getSitesId());
			}

			if (sitesAnnouncementEntity.getTitle() != null) {
				sitesAnnounementUpdate.set("title", sitesAnnouncementEntity.getTitle());
			}

			if (sitesAnnouncementEntity.getContent() != null) {
				sitesAnnounementUpdate.set("content", sitesAnnouncementEntity.getContent());
			}

			if (sitesAnnouncementEntity.getWarnLevel() != null) {
				sitesAnnounementUpdate.set("warnLevel", sitesAnnouncementEntity.getWarnLevel());
			}

			if (sitesAnnouncementEntity.getZoneProvince() != null) {
				sitesAnnounementUpdate.set("zoneProvince", sitesAnnouncementEntity.getZoneProvince());
			}

			if (sitesAnnouncementEntity.getZoneCity() != null) {
				sitesAnnounementUpdate.set("zoneCity", sitesAnnouncementEntity.getZoneCity());
			}

			if (sitesAnnouncementEntity.getZoneDistrict() != null) {
				sitesAnnounementUpdate.set("zoneDistrict", sitesAnnouncementEntity.getZoneDistrict());
			}

			if (sitesAnnouncementEntity.getZoneDistrict() != null) {

			}

			datastore.update(annQuery, sitesAnnounementUpdate);
			return true;

		}
		return false;
	}

	@Override
	public List<SitesAnnouncementEntity> findAnnByCity(PagingVo pvo, String zoneCity,String zoneDistrict) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Query<SitesAnnouncementEntity> sitesAnnQuery = datastore.find(SitesAnnouncementEntity.class);
		

		if (StringUtils.isNotBlank(zoneCity)) {
			sitesAnnQuery.field("zoneCity").equal(zoneCity);
		}
		
		long cout = 0L;
		
		if (StringUtils.isNotBlank(zoneDistrict)) {
			sitesAnnQuery.field("zoneDistrict").equal(zoneDistrict);
			cout = (int) sitesAnnQuery.count();
		}
		if(cout==0L){
			sitesAnnQuery= datastore.find(SitesAnnouncementEntity.class);
			sitesAnnQuery.field("zoneCity").equal(zoneCity);
		}
		
		
		sitesAnnQuery.field("pubStatus").equal("1");
		Long total = 0L;
		total = sitesAnnQuery.count();
		sitesAnnQuery.order("-pubTime").offset(pn).limit(ps);
		List<SitesAnnouncementEntity> content = sitesAnnQuery.asList();
		pvo.setTc(total);
		return content;
		
	}

	@Override
	public SitesAnnouncementEntity getSitesAnnounce(String id) {
		return  datastore.get(SitesAnnouncementEntity.class,id);
	}



}
